<template>
  <div>
    <div>
      <el-button size="mini" @click="() => triggerModify()">新增</el-button>
    </div>
    <el-table :data="dataList" size="mini" :loading="loading">
      
      <el-table-column label="id" prop="id">
        <template #default="scope">
          <div>
            {{ scope.row["id"] }}
          </div>
        </template>
      </el-table-column>
      
      <el-table-column label="dic_id" prop="dic_id">
        <template #default="scope">
          <div>
            {{ scope.row["dic_id"] }}
          </div>
        </template>
      </el-table-column>
      
      <el-table-column label="value" prop="value">
        <template #default="scope">
          <div>
            {{ scope.row["value"] }}
          </div>
        </template>
      </el-table-column>
      
      <el-table-column label="code" prop="code">
        <template #default="scope">
          <div>
            {{ scope.row["code"] }}
          </div>
        </template>
      </el-table-column>
      

      <el-table-column label="操作" fixed="right">
        <template #default="scope">
          <div>
            <el-button type="text" @click="() => triggerModify(scope.row)"
              >编辑</el-button
            >

            <el-button type="text" @click="() => triggerdeleteone(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="true"
      style="padding-top: 20px"
      background
      :page-size="queryParams.pageSize"
      layout="sizes,prev, pager, next"
      :total="total"
      @size-change="
        (pageSize) => {
          queryParams.pageSize = pageSize;
          dataList = [];
          getInfoList();
        }
      "
      @current-change="
        (pageNum) => {
          queryParams.pageNum = pageNum;
          dataList = [];
          getInfoList();
        }
      "
    ></el-pagination>
  </div>
</template>
<script setup>
import FormDialog from "../../components/FormDialog";
import { alertConfirm, alertWithPaylod } from "@/utils/alertUtils";
import axios from "axios";
import { ref, onMounted } from "vue";

const dataList = ref([]);
const total = ref(0);
const queryParams = ref({ pageNum: 1, pageSize: 10 });
const loading = ref(false);

async function getInfoList() {
  loading.value = true;
  var result = await axios
    .post("/api/tb_dictdetails/apis", {
      _name: "getlist",
      module: "",
      ...queryParams.value,
    })
    .then((res) => res.data);
  total.value = result?.count || result?.total;
  dataList.value = result?.data || [];
  loading.value = false;
}

async function triggerModify(row) {
  var result = await FormDialog({
    title: "管理",
    data() {
      return { ...row };
    },
    components: {
      modify: () => import("./modify.vue"),
    },
  });
  console.log("result :", result);
  if (result?.success) {
    getInfoList();
  }
}

async function triggerdeleteone(row) {
  await alertConfirm("确定删除吗？");
  var result = await axios
    .post("/api/tb_dictdetails/apis", {
      _name: "删除",
      module: "",
      ...row,
    })
    .then((res) => res.data);
  alertWithPaylod(result);
  if (result?.success) {
    getInfoList();
  }
}
onMounted(() => {
  getInfoList();
});
</script>
